package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.GovOffInfo;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ReportPublicField;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.GovOffInfoRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.strategy.StrategyService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.UUIDGenerator;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/09
 * @Description: 居住房屋基础资料报表
 */
//@Service("政府办公用房基础资料报表")
@Service("11")
public class GovOffInfoServiceImpl implements StrategyService {

    @Resource
    GovOffInfoRepository govOffInfoRepository;
    @Resource
    ReportPublicFieldRepository reportPublicFieldRepository;

    @Override
    public void reportImport(Workbook workbook, String xlrmId, String previousXlrmId, ReportPublicField reportPublicField) {
        if(!PlatformObjectUtils.isEmpty(previousXlrmId)){
            govOffInfoRepository.deleteByXlrmId(previousXlrmId);
            //删除报表公共字段
            reportPublicFieldRepository.deleteByXlrmId(previousXlrmId);
        }
        GovOffInfo govOffInfo = new GovOffInfo();
        govOffInfo.setXlrmId(xlrmId);
        //获得当前sheet工作表
        Sheet sheet = workbook.getSheetAt(0);
        //获得当前sheet的开始行
        int firstRowNum = sheet.getFirstRowNum();
        //获得当前sheet的结束行
        int lastRowNum = sheet.getLastRowNum();
        //循环除了第一行的所有行
        for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {

            String uuid = UUIDGenerator.getUUID();
            govOffInfo.setXgoiId(uuid);
            //获得当前行
            Row row = sheet.getRow(rowNum);
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            if (row == null) {
                continue;
            }
            if (rowNum == 1) {
                govOffInfo.setFillFormUser(hSSFDataFormatter.formatCellValue(row.getCell(1)));

                govOffInfo.setTel(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                govOffInfo.setFillFormTime(hSSFDataFormatter.formatCellValue(row.getCell(5)));
            }
            if (rowNum == 2) {
                govOffInfo.setProvince(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                govOffInfo.setCity(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                govOffInfo.setCounty(hSSFDataFormatter.formatCellValue(row.getCell(5)));

                reportPublicField.setProvince(govOffInfo.getProvince());
                reportPublicField.setCity(govOffInfo.getCity());
                reportPublicField.setCounty(govOffInfo.getCounty());
                //默认删除
                reportPublicField.setDelFlag(YNEnum.Y.toString());
            }
            if (rowNum == 5) {
                govOffInfo.setUnitPrice1(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                govOffInfo.setUnitPrice2(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                govOffInfo.setUnitPrice31(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                govOffInfo.setUnitPrice32(hSSFDataFormatter.formatCellValue(row.getCell(8)));
                govOffInfo.setUnitPrice4(hSSFDataFormatter.formatCellValue(row.getCell(9)));
                govOffInfo.setUnitPrice51(hSSFDataFormatter.formatCellValue(row.getCell(10)));
                govOffInfo.setUnitPrice52(hSSFDataFormatter.formatCellValue(row.getCell(11)));
                govOffInfo.setUnitPrice53(hSSFDataFormatter.formatCellValue(row.getCell(12)));
                govOffInfo.setUnitPrice6(hSSFDataFormatter.formatCellValue(row.getCell(13)));
                govOffInfo.setUnitPrice7(hSSFDataFormatter.formatCellValue(row.getCell(14)));
            }
            if (rowNum > 5) {
                String s = hSSFDataFormatter.formatCellValue(row.getCell(1));
                if(s.equals("")){
                    continue;
                }
                govOffInfo.setUnitName(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                govOffInfo.setAffiliatedUnit(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                govOffInfo.setPurpose(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                govOffInfo.setIndoorAssetAvg(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                govOffInfo.setArea1(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                govOffInfo.setArea2(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                govOffInfo.setArea31(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                govOffInfo.setArea32(hSSFDataFormatter.formatCellValue(row.getCell(8)));
                govOffInfo.setArea4(hSSFDataFormatter.formatCellValue(row.getCell(9)));
                govOffInfo.setArea51(hSSFDataFormatter.formatCellValue(row.getCell(10)));
                govOffInfo.setArea52(hSSFDataFormatter.formatCellValue(row.getCell(11)));
                govOffInfo.setArea53(hSSFDataFormatter.formatCellValue(row.getCell(12)));
                govOffInfo.setArea6(hSSFDataFormatter.formatCellValue(row.getCell(13)));
                govOffInfo.setArea7(hSSFDataFormatter.formatCellValue(row.getCell(14)));
                govOffInfoRepository.save(govOffInfo);
            }
        }
        reportPublicFieldRepository.save(reportPublicField);
    }
}
